package com.cec.service.admin.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cec.dto.VolunteerDTO;
import com.cec.entity.User;
import com.cec.entity.Volunteer;
import com.cec.exception.OperationDataException;
import com.cec.mapper.admin.UserMapper;
import com.cec.mapper.admin.VolunteerMapper;
import com.cec.result.ResultCode;
import com.cec.service.admin.UserService;
import com.cec.service.admin.VolunteerService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cec.result.Result;
import com.cec.vo.VolunteerVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lt
 * @since 2024-10-25
 */
@Slf4j
@Service
public class VolunteerServiceImpl extends ServiceImpl<VolunteerMapper, Volunteer> implements VolunteerService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public Result pageQueryVolunteerActivityReg(VolunteerDTO volunteerDTO) {
        QueryWrapper<VolunteerDTO> wrapper = new QueryWrapper<>();
        if (volunteerDTO.getUserName()!=null && !"".equals(volunteerDTO.getUserName()))
            wrapper.like("u.user_name",volunteerDTO.getUserName());
        if (volunteerDTO.getActivityTypeId()!=null)
            wrapper.eq("a.activity_type_id",volunteerDTO.getActivityTypeId());
        wrapper.eq("v.deleted",0)
                .orderByDesc("v.create_time");
        IPage<VolunteerVO> activityVOIPage = baseMapper.pageQueryVolunteerList(new Page<>(volunteerDTO.getPage(),volunteerDTO.getPageSize()),wrapper);
        //IPage<Volunteer> activityVOIPage = baseMapper.selectPage(new Page<>(1, 5), null);
        log.info("activityVOIPage.getRecords()-->{}", activityVOIPage.getRecords());
        log.info("activityVOIPage.getTotal()-->{}", activityVOIPage.getTotal());
        return Result.success(activityVOIPage.getRecords(),activityVOIPage.getTotal());
    }

    @Override
    public Result logicDeleteVolunteerActivityRegById(Integer id) {
        int i = baseMapper.deleteById(id);
        if (i>0)
            return Result.success();
        throw new OperationDataException(ResultCode.OPERATION_DATA_ERROR);
    }

    @Override
    public Result logicBatchDeleteVolunteerActivityRegByIds(List<Integer> ids) {
        int i = baseMapper.deleteBatchIds(ids);
        if (i>0)
            return Result.success();
        throw new OperationDataException(ResultCode.OPERATION_DATA_ERROR);
    }

    @Override
    public Result updateVolunteerActivityReg(VolunteerDTO volunteerDTO) {
        Volunteer volunteer = baseMapper.selectById(volunteerDTO.getId());
        User user = userMapper.selectById(volunteer.getUserId());
        if (user!=null && volunteerDTO.getServiceDuration()!=0){
            Double serviceDuration = user.getServiceDuration();
            user.setServiceDuration(serviceDuration + volunteerDTO.getServiceDuration());
            userMapper.updateById(user);
        }

        if (user!=null && volunteerDTO.getCreditScore()!=0){
            User setUserCreditScore = user.setCreditScore(volunteerDTO.getCreditScore());
            userMapper.updateById(setUserCreditScore);
        }

        UpdateWrapper<Volunteer> updateWrapper = new UpdateWrapper<>();
        if (volunteerDTO.getStatus()!=null )
            updateWrapper.set("status",volunteerDTO.getStatus());
        if (volunteerDTO.getEvaluate()!=null && !"".equals(volunteerDTO.getEvaluate()))
            updateWrapper.set("evaluate",volunteerDTO.getEvaluate());
        if (volunteerDTO.getOpinion()!=null && !"".equals(volunteerDTO.getOpinion()))
            updateWrapper.set("opinion",volunteerDTO.getOpinion());
        updateWrapper.eq("id",volunteerDTO.getId());
        int i = baseMapper.update(new Volunteer(), updateWrapper);
        if (i>0)
            return Result.success();
        throw new OperationDataException(ResultCode.OPERATION_DATA_ERROR);
    }
}
